Ext.ns('AIR2.Organization');
/***************
 * Organization creation modal window
 *
 * Opens a modal window allowing the creation of new organizations or child
 * organizations.
 *
 * @function AIR2.Organization.Create
 * @cfg {HTMLElement} originEl (optional) origin element to animate from
 * @cfg {Boolean} redirect (default false) redirect on success
 * @cfg {String} parentUUID (optional) uuid of parent org
 * @cfg {Function} callback (optional) function passed boolean success
 *
 */
AIR2.Organization.Create = function(cfg) {
    var w = new AIR2.UI.Window({
        title: 'Create Organization',
        iconCls: 'air2-icon-organization',
        closeAction: 'close',
        width: 290,
        height: 185,
        formAutoHeight: true,
        items: {
            xtype: 'form',
            unstyled: true,
            style: 'padding: 10px 10px 0',
            labelWidth: 70,
            defaults: {
                xtype: 'textfield',
                allowBlank: false,
                width: 180,
                msgTarget: 'under'
            },
            items: [{
                xtype: 'air2remotetext',
                fieldLabel: 'Short Name',
                name: 'org_name',
                remoteUrl: AIR2.HOMEURL + '/organization/name.json',
                autoCreate: {tag: 'input', type: 'text', maxlength: '32'}
            },{
                fieldLabel: 'Long Name',
                name: 'org_display_name',
                maxLength: 128
            },{
                fieldLabel: 'Logo URL',
                allowBlank: true,
                name: 'org_logo_uri',
                vtype: 'url',
                maxLength: 255
            },{
                xtype: 'hidden', name: 'org_parent_uuid', value: cfg.parentUUID
            }],
            bbar: [{
                xtype: 'air2button',
                air2type: 'SAVE',
                air2size: 'MEDIUM',
                text: 'Save',
                handler: function() {
                    var f = w.get(0).getForm(), el = w.get(0).el;

                    // validate and fire the ajax save
                    if (f.isValid()) {
                        el.mask('Saving');
                        Ext.Ajax.request({
                            url: AIR2.HOMEURL + '/organization.json',
                            params: {radix: Ext.encode(f.getFieldValues())},
                            callback: function(opt, success, rsp) {
                                if (success) {
                                    var d = Ext.decode(rsp.responseText);
                                    if (d.success && d.radix.org_uuid) {
                                        if (cfg.callback) cfg.callback(true);
                                        if (cfg.redirect) {
                                            location.href = AIR2.HOMEURL + '/organization/' + d.radix.org_uuid;
                                        }
                                        else {
                                            w.close();
                                        }
                                        return;
                                    }
                                }
                                // failed
                                if (cfg.callback) cfg.callback(true, rsp.responseText);
                                el.unmask();
                                alert("TODO: FAILED\n"+rsp.responseText);
                            }
                        });
                    }
                }
            },' ',{
                xtype: 'air2button',
                air2type: 'CANCEL',
                air2size: 'MEDIUM',
                text: 'Cancel',
                handler: function() {w.close();}
            }]
        }
    });
    if (cfg.originEl) w.show(cfg.originEl);
    else w.show();

    return w;
}
